package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.ChefDuelData;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2Spoon;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuel2;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuelActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_CHEF_DUEL2_PRE_JOIN_TABLE, desc = "入座试吃（预判断）")
public class ChefDuel2PreJoinTableCmd extends AbstractCommand {

    //本服的活动
    int activityId;
    long homeUserId;
    int tableId;
    int spoonId;
    //可赴宴的活动
    int otherActivityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        ChefDuelProto.ChefDuel2PreJoinTableReqMsg reqMsg = ChefDuelProto.ChefDuel2PreJoinTableReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        homeUserId = reqMsg.getUserId();
        tableId = reqMsg.getTableId();
        spoonId = reqMsg.getSpoonId();
        if(reqMsg.hasOtherActivityId()){
            otherActivityId = reqMsg.getOtherActivityId();
        }

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        ActivityInfo activityInfo = ChefDuelActivityMgr.getInTimeActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
            return;
        }
        if(!ChefDuelActivityMgr.isActivityMatchTime(activityInfo)){
            ret = GameErrorCode.E_CHEF_DUEL_NOT_IN_MATCH_TIME;
            return;
        }
        ChefDuel2Spoon cookBoy2Spoon = ChefDuelActivityMgr.getChefDuel2Spoon(activityId,spoonId);
        if(cookBoy2Spoon == null){
            ret = GameErrorCode.E_CHEF_DUEL2_SPOON_NOT_FOUND;
            return;
        }
        if(!player.getModule(ChefDuelModule.class).isAble(activityId)){
            ret = GameErrorCode.E_CHEF_DUEL2_USER_NOT_ABLE;
            return;
        }

        if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cookBoy2Spoon.getConsume()))){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        if(otherActivityId > 0 && otherActivityId != activityId){
            //赴宴范围的试吃
            OtherActivityChefDuel2 otherActivityChefDuel2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(otherActivityId);
            if(otherActivityChefDuel2 != null){
                ChefDuelData duelData = ChefDuelMgr.getUserChefDuel2DataNoLock(activityId, player);
                ret = otherActivityChefDuel2.sendPreJoinMsg(player.getUserId(),homeUserId,tableId,spoonId, activityId, duelData.getCampId());
                if(ret == 0){
                    //赴宴范围的场景
                    otherActivityChefDuel2.addSceneUser(player.getUserId(), homeUserId);
                }
            }
        }else {
            ret = ChefDuelMgr.sendPreJoinMsg(activityId,player.getUserId(),homeUserId,tableId,spoonId, activityId);
            ChefDuelMgr.addChefDuel2SceneUser(activityId, player.getUserId(), homeUserId);
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ChefDuelProto.ChefDuel2PreJoinTableRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2PreJoinTableRespMsg.newBuilder();
            respMsg.addAllForbidSpoonId(player.getModule(ChefDuelModule.class).getForbidSpoonIdList(activityId));
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CHEF_DUEL2_JOIN_TABLE, respMsg);
        }
    }
}
